package com.freesky.quartz.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.freesky.biz.activity.mapper.AlipayBillTaskMapper;
import com.freesky.biz.activity.mapper.CouponBillMapper;
import com.freesky.biz.activity.mapper.CouponDetailMapper;
import com.freesky.biz.activity.mapper.ProjectMapper;
import com.freesky.common.core.redis.RedisCache;
import com.freesky.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 *  统计券的每天发放情况，以及自动更新活动信息
 */
@Component("couponDayReportTask")
public class CouponDayReportTask {
    private static final Logger logger = LoggerFactory.getLogger(CouponDayReportTask.class);

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private CouponBillMapper couponBillMapper;

    @Autowired
    private AlipayBillTaskMapper billTaskMapper;

    @Autowired
    private CouponDetailMapper couponDetailMapper;

    @Autowired
    private ProjectMapper projectMapper;

    private String REPORT_COUPON_KEY = "REPORT:DAY:COUPON";
    private String REPORT_BILL_KEY = "REPORT:DAY:BILL";
    private String REPORT_REFUND_BILL_KEY = "REPORT:DAY:REFUNDBILL";

    private String REPORT_GRANT_KEY = "REPORT:DAY:GRANT";

    public void taskProcess() {
        logger.info("CouponDayReport task start");
        String minUpdateDate = DateUtil.format(DateUtil.offsetDay(new Date(), -7),"yyyy-MM-dd");

        reportDayCoupon(minUpdateDate);
        reportDayBill(minUpdateDate);
        reportDayRefundBill(minUpdateDate);

        projectMapper.updateProjectStatus();

        reportDayGrant(minUpdateDate);
        billTaskMapper.updateAlipayConfigReport();

        //更新微信核销状态
        couponBillMapper.updateConsumStatus();
        billTaskMapper.updateConsumeStatus();

        //南平统计
        //couponBillMapper.updateNanpingReferrer();

        //南平工银社区统计（停用：活动已结束：2025-09-14）
        //couponBillMapper.updateNanpingPartner();
    }

    public void reportDayCoupon(String minUpdateDate){
        //微信立减金
        String prevUpdateDate = redisCache.getCacheObject(REPORT_COUPON_KEY);
        if(StringUtils.isEmpty(prevUpdateDate)){
            prevUpdateDate = minUpdateDate;
        }

        String lastUpdateDate = couponBillMapper.selectMaxUpdateDate();
        if(StringUtils.isEmpty(lastUpdateDate)){
            lastUpdateDate = minUpdateDate;
        }

        if(lastUpdateDate.equalsIgnoreCase(prevUpdateDate)){
            logger.info("没有新的数据，无须统计："+prevUpdateDate+","+lastUpdateDate);
            return;
        }

        String minConsume = couponBillMapper.selectMinConsumeTime(prevUpdateDate);
        if(StringUtils.isEmpty(minConsume)){
            logger.info("暂无找到立减金数据，无法统计："+prevUpdateDate);
            return;
        }

        String startDt = minConsume.substring(0,10);
        String endDt = lastUpdateDate.substring(0,10) +" 23:59:59";
        couponBillMapper.addCouponReportDay(startDt,endDt);
        couponBillMapper.updateStockMonitorReport(startDt,endDt);

        redisCache.setCacheObject(REPORT_COUPON_KEY,lastUpdateDate);
    }

    public void reportDayBill(String minUpdateDate){
        //支付宝核销数据
        String prevUpdateDate = redisCache.getCacheObject(REPORT_BILL_KEY);
        if(StringUtils.isEmpty(prevUpdateDate)){
            prevUpdateDate = minUpdateDate;
        }

        String lastUpdateDate = billTaskMapper.selectMaxBillUpdateDate();
        if(StringUtils.isEmpty(lastUpdateDate)){
            lastUpdateDate = minUpdateDate;
        }

        if(lastUpdateDate.equalsIgnoreCase(prevUpdateDate)){
            logger.info("没有新的数据，无须统计："+prevUpdateDate+","+lastUpdateDate);
            return;
        }

        String minConsume = billTaskMapper.selectMinBillConsumeTime(prevUpdateDate);
        if(StringUtils.isEmpty(minConsume)){
            logger.info("暂无找到支付宝核销数据，无法统计："+prevUpdateDate);
            return;
        }

        String startDt = minConsume.substring(0,10);
        String endDt = lastUpdateDate.substring(0,10) +" 23:59:59";
        billTaskMapper.addBillReportDay(startDt,endDt);
        billTaskMapper.addAlipayActivityConfig(startDt,endDt);
        redisCache.setCacheObject(REPORT_BILL_KEY,lastUpdateDate);
    }

    public void reportDayRefundBill(String minUpdateDate){
        //支付宝退款数据
        String prevUpdateDate = redisCache.getCacheObject(REPORT_REFUND_BILL_KEY);
        if(StringUtils.isEmpty(prevUpdateDate)){
            prevUpdateDate = minUpdateDate;
        }

        String lastUpdateDate = billTaskMapper.selectMaxRefundBillUpdateDate();
        if(StringUtils.isEmpty(lastUpdateDate)){
            lastUpdateDate = minUpdateDate;
        }

        if(lastUpdateDate.equalsIgnoreCase(prevUpdateDate)){
            logger.info("没有新的数据，无须统计："+prevUpdateDate+","+lastUpdateDate);
            return;
        }
        String minConsume = billTaskMapper.selectMinBillRefundTime(prevUpdateDate);
        if(StringUtils.isEmpty(minConsume)){
            logger.info("暂无找到支付宝退款数据，无法统计："+prevUpdateDate);
            return;
        }

        String startDt = minConsume.substring(0,10);
        String endDt = lastUpdateDate.substring(0,10) +" 23:59:59";
        billTaskMapper.addRefundBillReportDay(startDt,endDt);
        billTaskMapper.addAlipayRefundActivityConfig(startDt,endDt);
        redisCache.setCacheObject(REPORT_REFUND_BILL_KEY,lastUpdateDate);
    }

    public void reportDayGrant(String minUpdateDate){
        //微信立减金
        String prevUpdateDate = redisCache.getCacheObject(REPORT_GRANT_KEY);
        if(StringUtils.isEmpty(prevUpdateDate)){
            prevUpdateDate = minUpdateDate;
        }

        String lastUpdateDate = DateUtil.format(DateUtil.offsetDay(new Date(), -1),"yyyy-MM-dd");

        String startDt = prevUpdateDate.substring(0,10);
        String endDt = lastUpdateDate.substring(0,10) +" 23:59:59";

        couponDetailMapper.updateReportGrantCount(startDt,endDt);
        couponDetailMapper.updateReportGrantCount(startDt,endDt);

        redisCache.setCacheObject(REPORT_GRANT_KEY,lastUpdateDate);
    }

}
